package com.rainy.guoran.business.manager.controller;

import com.rainy.guoran.business.manager.controller.vo.RoleRespVO;
import com.rainy.guoran.business.manager.controller.vo.RoleSaveReqVO;
import com.rainy.guoran.business.manager.entity.CommonResult;
import com.rainy.guoran.business.manager.entity.Role;
import com.rainy.guoran.business.manager.services.RoleService;
import io.swagger.v3.oas.annotations.ExternalDocumentation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import static com.rainy.guoran.business.manager.entity.CommonResult.success;

@AllArgsConstructor
@RestController
@RequestMapping("/system/role")
@Tag(
        name = "RoleController",
        description = "角色管理接口",
        externalDocs = @ExternalDocumentation(
                description = "这是一个接口文档介绍",
                url = "https://rainy.guoran.com/"))
public class RoleController {

    private final RoleService roleService;

    @PostMapping("/create")
    @Operation(summary = "创建角色")
    public CommonResult<Long> createRole(@Valid @RequestBody RoleSaveReqVO createReqVO) {
        return success(roleService.createRole(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "修改角色")
    public CommonResult<Boolean> updateRole(@Valid @RequestBody RoleSaveReqVO updateReqVO) {
        roleService.updateRole(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除角色")
    @Parameter(name = "id", description = "角色编号", required = true, example = "1024")
    public CommonResult<Boolean> deleteRole(@RequestParam("id") Long id) {
        roleService.deleteRole(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得角色信息")
    public CommonResult<RoleRespVO> getRole(@RequestParam("id") Long id) {
        Role role = roleService.getRole(id);
        RoleRespVO roleRespVO = new RoleRespVO();
        BeanUtils.copyProperties(role, roleRespVO);
        return success(roleRespVO);
    }
}
